/*!
\file DManager.cpp
\brief Contains implementation of DManager.
\ingroup DOT
*/

#include"DManager.h" 
#include<stdio.h>
DManager::DManager( QStringList stringList ){
	m_communicator = new ActionCommunicator;
	connect( m_communicator, SIGNAL(nodeAdd(QString)), this, SLOT(_addNode(QString)) );
	connect( m_communicator, SIGNAL(edgeAdd(QString)), this, SLOT(_addEdge(QString)) );
	connect( m_communicator, SIGNAL( nodeSetOption(int, QString, QString) ), this, SLOT( _setNodeOption(int, QString, QString) ) );
	connect( m_communicator, SIGNAL( edgeSetOption(int, QString, QString) ), this, SLOT( _setEdgeOption(int, QString, QString) ) );
	connect( m_communicator, SIGNAL(nodeRemove(int)), this, SLOT(_removeNode(int)) );
	connect( m_communicator, SIGNAL(edgeRemove(int)), this, SLOT(_removeEdge(int)) );
	
	int i = 1;
	m_first = stringList[0];
	printf("Starting nodes.\n");
	while( stringList[i] != QString("/*edges*/") ){
		if( stringList[i].trimmed().startsWith("/*") ){
			i++;
			continue;
		}
		m_nodeData.push_back( DNodeData(stringList[i]) );
		i++;
	}
	i++;
	printf("Starting edges.\n");
	while( !stringList[i].trimmed().startsWith("}") ){
		m_edgeData.push_back( DEdgeData(stringList[i]) );
		i++;
	}
	m_last = stringList[i];
	printf("Done\n");
}

DManager::~DManager(){
	delete m_communicator;
}

QString DManager::toDOT()const{
	bool directed = true;
	if( m_first.trimmed().startsWith("graph") )
		directed = false;
	
	QStringList ret;
	ret.push_back(m_first);
	
	ret.push_back( "size = \"8,8\"" );
	
	for( int i = 0; i < m_nodeData.size(); i++ )
		ret.push_back(m_nodeData[i].toDOT());
	for( int i = 0; i < m_edgeData.size(); i++ )
		ret.push_back(m_edgeData[i].toDOT(directed));
	ret.push_back(m_last);
	return ret.join("\n");
}

QPixmap DManager::toQPixmap()const{
	QString dot = toDOT();
	printf("\nShowing graph:\n");
	printf("%s\n\n", dot.toAscii().data());
	QFile *tempFile = new QFile( ".temp.dot" );
	tempFile->open( QIODevice::WriteOnly | QIODevice::Text );
	QTextStream textStream(tempFile);
	textStream << dot;
	textStream.flush();
	tempFile->close();
	delete tempFile;
	
	QProcess::execute( "dot -Tpng .temp.dot -o .temp.png" );
	
	QPixmap result( ".temp.png" );

#ifdef linux
	QProcess::execute( "rm .temp.dot" );
	QProcess::execute( "rm .temp.png" );
#endif
#ifndef linux
	QProcess::execute( "del .temp.dot" );
	QProcess::execute( "del .temp.png" );
#endif
	
	return result;
}

void DManager::_addNode( QString nodeData ){
	m_nodeData.push_back( DNodeData(nodeData) );
}

void DManager::_addEdge( QString edgeData ){
	m_edgeData.push_back( DEdgeData(edgeData) );
}

void DManager::_setNodeOption( int nodeID, QString option, QString value ){
	bool found = false;
	for( int i = 0; i < m_nodeData.size() && !found; i++ ){
		if( m_nodeData[i].id() == nodeID ){
			found = true;
			m_nodeData[i].setOption( option.trimmed(), value );
		}
	}
}

void DManager::_setEdgeOption( int edgeID, QString option, QString value ){
	bool found = false;
	for( int i = 0; i < m_edgeData.size() && !found; i++ ){
		if( m_edgeData[i].id() == edgeID ){
			found = true;
			m_edgeData[i].setOption( option.trimmed(), value );
		}
	}
}

void DManager::_removeNode( int nodeID ){
	for( int i = 0; i < m_nodeData.size(); i++ ){
		if( m_nodeData[i].id() == nodeID ){
			m_nodeData[i] = m_nodeData[0];
			m_nodeData.pop_front();
			break;
		}
	}
}

void DManager::_removeEdge( int edgeID ){
	for( int i = 0; i < m_edgeData.size(); i++ ){
		if( m_edgeData[i].id() == edgeID ){
			m_edgeData[i] = m_edgeData[0];
			m_edgeData.pop_front();
			break;
		}
	}
}
